package huu.phong.youtube;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import javax.swing.JComponent;
import javax.swing.JToolTip;
import javax.swing.SwingUtilities;
import javax.swing.plaf.metal.MetalToolTipUI;

public class VideoToolTipUI extends MetalToolTipUI{
	private BufferedImage image;
	private Dimension dimension = new Dimension(1, 1);
	
	public VideoToolTipUI(){
		
	}
	
	public void paint(Graphics g, JComponent c) {
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
		g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
		if (image != null){
			g2.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
		}else{
			String tooltip = ((JToolTip)c).getTipText();
			if (tooltip != null){
				g2.setFont(YouTube.DEFAULT_FONT.deriveFont(Font.BOLD, 20f));
				dimension = new Dimension(SwingUtilities.computeStringWidth(g2.getFontMetrics(), tooltip), g2.getFontMetrics().getAscent() - g2.getFontMetrics().getDescent());
				dimension.width += 20;
				dimension.height += 10;
				g2.setColor(Color.BLACK);
				g2.fillRect(0, 0, dimension.width, dimension.height);
				g2.setColor(Color.WHITE);
				g2.drawString(tooltip, 10, dimension.height - 5);
			}
		}
	}
	
	public Dimension getPreferredSize(JComponent c){
		if (image == null) return dimension;
		return new Dimension(image.getWidth(), image.getHeight());
	}
	
	public void setImage(BufferedImage image){
		this.image = image;
	}
}
